package com.gzsxy.excel.util;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;


import com.gzsxy.excel.dropDownListHandler.ProductWeeklyHandler;
import com.gzsxy.excel.dropDownListHandler.SysUserSheetWriteHandler;
import com.gzsxy.excel.dropDownListHandler.TeamUserSheetWriteHandler;
import com.gzsxy.excel.entity.ProductWeeklyReportTo;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

public class ExcelUtils {
    //导出并下载xlsx
    public static void downloadUnfilledToXlsx(String excelName, HttpServletResponse response, Class cla, List list, String title) throws Exception {
        // 这里注意 使用swagger 会导致各种问题，easyexcel官方文档推荐直接用浏览器或者用postman测试
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode(excelName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)13);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        if(excelName == "新增用户模板"){
            EasyExcel.write(response.getOutputStream(), cla).registerWriteHandler(new SysUserSheetWriteHandler()).sheet(excelName).doWrite(list);
        }else if(excelName == "新增团队成员模板"){
            EasyExcel.write(response.getOutputStream(), cla).registerWriteHandler(new TeamUserSheetWriteHandler()).sheet(excelName).doWrite(list);
        }
        if(cla == ProductWeeklyReportTo.class){
//            EasyExcel.write(response.getOutputStream(), cla).registerWriteHandler(new ProductWeeklyHandler()).sheet(excelName).useDefaultStyle(true).relativeHeadRowIndex(1).doWrite(list);

            EasyExcel.write(response.getOutputStream(), cla)
                    .registerWriteHandler(new ProductWeeklyHandler(title))
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    //设置默认样式
                    .useDefaultStyle(true)
                    //设置写入头信息开始行数
                    .relativeHeadRowIndex(1)
                    .sheet(excelName)
                    .doWrite(list);
        }
        EasyExcel.write(response.getOutputStream(), cla).sheet(excelName).doWrite(list);
    }

}